package com.springboot.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = true)
public class SwaggerConfig {

    @Bean
    public Docket createWebRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("web模块")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.springboot.controller.web"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createBackRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("后台模块")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.springboot.controller.back"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createOpenRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("开放模块")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.springboot.controller.open"))
                .paths(PathSelectors.any())
                .build();
    }



    // 创建appApi的基本信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("农业官网接口")
                .description("农业官网接口完成进度 APIs")
                .termsOfServiceUrl("http://www.agriculture.com/")
                .contact(new Contact("coDing", "com.springboot", "1290830061@qq.com"))
                .version("1.0.0")
                .build();
    }

    /**
     * swagger ui资源映射
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * swagger-ui.html路径映射，浏览器中使用/api-docs访问
     * http://localhost:8080/swagger-ui.html
     *  文档地址 http://localhost:8888/doc.html
     */
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/doc.html","/swagger-ui.html");
    }
}